import Vue from 'vue'
import Vuex from 'vuex'
import {Notification} from 'element-ui'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    showFooter: true,
    changeableNum: 'admin',
    token: window.localStorage.getItem('token'),
    auth: false,
    userInfo: {},
    currentMenu: '',  // 当前选中菜单
    currentOpenSub: [], // 当前打开的submenu
    newsNum: 0  // 新消息数
  },
  getters: {
    getUserInfo (state) {
      return state.userInfo
    },
    getAuth (state) {
      return state.auth
    },
    getToken (state) {
      return state.token ? state.token : window.localStorage.getItem('token')
    },
    isShow (state) {
      return state.showFooter
    },
    getChangeAbleNum (state) {
      return state.changeableNum
    },
    getCurrentMenu (state) {
      return state.currentMenu
    },
    getCurrentOpenSub (state) {
      return state.currentOpenSub
    },
    getNewsNum (state) {
      return state.newsNum
    }
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      window.localStorage.setItem('token', token)
    },
    clearToken (state) {
      state.token = ''
      window.localStorage.setItem('token', '')
    },
    setUserInfo (state, userInfo) {
      state.userInfo = userInfo
      state.auth = true // 获取用户信息的同时将auth标记为true，当然也可以直接判断userInfo
    },
    clearUserInfo (state) {
      state.userInfo = {}
      state.auth = false
    },
    changeShow (state, visible) {
      state.showFooter = visible
    },
    changeNum (state, num) {
      state.changeableNum = num
    },
    setCurrentMenu (state, currentMenu) {
      state.currentMenu = currentMenu
    },
    setCurrentOpenSub (state, currentOpenSub) {
      state.currentOpenSub = currentOpenSub
    },
    setNewsNum (state, newsNum) {
      state.newsNum += Number(newsNum)
    }
  },
  actions: {
    // 设置token
    setToken (context, token) {
      context.commit('setToken', token)
    },
    // 清除token
    clearToken (context) {
      context.commit('clearToken')
    },
    // 设置userInfo
    setUserInfo (context, userInfo) {
      context.commit('setUserInfo', userInfo)
    },
    // 清楚userInfo
    clearUserInfo (context) {
      context.commit('clearUserInfo')
    },
    changeShow1 (context, visible) {
      context.commit('changeShow', visible)
    },
    changeNum1 (context, num) {
      context.commit('changeNum', num)
    },
    setCurrentMenu (context, currentMenu) {
      context.commit('setCurrentMenu', currentMenu)
    },
    setCurrentOpenSub (context, currentOpenSub) {
      context.commit('setCurrentOpenSub', currentOpenSub)
    },
    setNewsNum (context, newsNum) {
      context.commit('setNewsNum', newsNum)
      Notification({
        title: '提示',
        message: '您有新的订单请及时处理',
        duration: 2500
      })
    }
  }
})
